package dLogistic.domain.model.Paczka;

import dLogistic.domain.model.BaseAggregateRoot;
import org.hibernate.SessionFactory;

import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.Table;

/**
 * Created by Przemek on 2014-10-23.
 */
@Entity
@Table(name = "Paczka")
public class PaczkaAggr extends BaseAggregateRoot {
    private static SessionFactory factory;
// każdy agregat pochodzi z lasy baseAggregateRoot

    @Id
    private String uuid;    //uuid

    private String nadawca; //uuid
    private String adresat; //uuid

    public PaczkaAggr(String nadawca, String adresat, String uuid){
        this.nadawca = nadawca;
        this.adresat = adresat;
        this.uuid = uuid;
    }
/*
    public void WyznaczTrasePaczki(){
        KlientRepository klientRepository = new KlientRepository();
        //MagazynRepository magazynRepository = new MagazynRepository();
        MagazynAggr magNadAggr = null;
        MagazynAggr magAdrAggr = null;
        KlientAggr nadawcaAggr = klientRepository.znajdz(this.nadawca);
        KlientAggr adresatAggr = klientRepository.znajdz(this.adresat);

        if (nadawcaAggr != null){
            magNadAggr = klientRepository.najblizszyMagazyn(nadawcaAggr);
        }
        if (adresatAggr != null){
            magAdrAggr = klientRepository.najblizszyMagazyn(adresatAggr);
        }

        factory = HibernateUtil.getSessionFactory();
        Session session = factory.getCurrentSession();
        Transaction transaction = session.beginTransaction();
        if (magNadAggr != null){
            TrasaPaczki trasaPaczki1 = new TrasaPaczki(this.uuid(), nadawcaAggr.uuid(), magNadAggr.uuid(), 0 , 0);
            session.saveOrUpdate(trasaPaczki1);
        }
        if (magAdrAggr != null){
            TrasaPaczki trasaPaczki2 = new TrasaPaczki(this.uuid(), magAdrAggr.uuid(), adresatAggr.uuid(), 2 , 0);
            session.saveOrUpdate(trasaPaczki2);
        }

        if (magNadAggr != null && magAdrAggr != null){
            TrasaPaczki trasaPaczki2 = new TrasaPaczki(this.uuid(), magAdrAggr.uuid(), magNadAggr.uuid(), 2 , 0);
            session.saveOrUpdate(trasaPaczki2);

        }

        transaction.commit();
    }
*/
    public String uuid(){
        return uuid;
    }

    public String nadawca(){
        return nadawca;
    }
    public String adresat(){
        return adresat;
    }

    public PaczkaAggr(){

    }
}
